﻿using Shop.Model.DB;
using Shop.RemoteModel.ActivityDiscount.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Shop.Repository.lmpl
{
    internal class ActivityDiscountRepository : ShopBaseRepository<DBShopActivityDiscount, long>, IActivityDiscountRepository
    {
        public ActivityDiscountRepository (ISqlClientFactory factory) : base(factory)
        {
        }
        public void Sync (long activityId, DiscountSet discount, string show)
        {
            DBShopActivityDiscount[] adds = discount.Items.ConvertMap<DiscountItem, DBShopActivityDiscount>();
            adds.ForEach(c =>
            {
                c.Threshold = discount.Threshold;
                c.Id = IdentityHelper.CreateId();
                c.ActivityId = activityId;
            });
            ISqlQueue<DBShopActivityDiscount> queue = base.BeginQueue();
            queue.Insert(adds);
            long[] ids = base.Gets(a => a.ActivityId == activityId, a => a.Id);
            if (!ids.IsNull())
            {
                queue.Delete(a => ids.Contains(a.Id));
            }
            queue.UpdateOneColumn<DBShopActivity>(c => c.DiscountShow == show, c => c.Id == activityId);
            queue.Submit();
        }
    }
}
